# coding:utf-8
import os
import random
from keras.preprocessing.image import ImageDataGenerator
from PIL import Image
def resize(img):
    image = Image.open(img)
    w, h = image.size
    print(image.size)
    w = int(w)
    h = int(h)
    # 拉伸成宽为w的正方形
    out_ww = image.resize((int(w), int(w)))
    print(out_ww.size)
    out_ww.show()

    out_ww.save("sf.jpg",quality=100)


def read(img):
    im=Image.open(img)
    print(im.size)
    im_rotate = im.rotate(-30,fillcolor="#475822")
    im = im.transpose(Image.FLIP_TOP_BOTTOM)
    im_transpose = im.transpose(Image.ROTATE_90)
    im_transpose.show()
    # im_rotate.show()


class foxImgEhancer(object):
    def __init__(self,dir,rotate=1,horizontal_flip=False,vertical_flip=False):
        self.dir = dir
        self.rotate = rotate
        self.horizontal_flip=horizontal_flip,
        self.vertical_flip=vertical_flip

    def gen_random(self):
        alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
        f = [ alpha[random.randint(0,len(alpha)-1)] for _ in range(12)]
        return ''.join(f)

    def flow(self,save_to_dir,batch=1,necessary=False):
        for img in os.listdir(self.dir):
            path = os.path.join(self.dir,img)
            im = self._read(path)
            if necessary:
                self._rotate(im, batch, save_to_dir)
                self._transpose(im, batch, save_to_dir)
                self._deal(im,batch,save_to_dir)
            else:
                self._rotate(im,batch,save_to_dir)
                self._transpose(im,batch,save_to_dir)

    def _deal(self,im,batch,save_to_dir):
        for rotate in range(self.rotate):
            for bat in range(batch):
                im_rotate=im.rotate(rotate)
                im_rotate.save(os.path.join(save_to_dir,self.gen_random()+".jpg"))
                self._transpose(im_rotate,batch,save_to_dir)

    def _read(self,path):
        return Image.open(path)

    def _transpose(self,im,batch,savedir):
        if self.horizontal_flip:
            for bat in range(batch):
                im_transpose = im.transpose(Image.FLIP_LEFT_RIGHT)
                im_transpose.save(os.path.join(savedir, self.gen_random() + ".jpg"))
                im = im_transpose
        if self.vertical_flip:
            for bat in range(batch):
                im_transpose = im.transpose(Image.FLIP_TOP_BOTTOM)
                im_transpose.save(os.path.join(savedir, self.gen_random() + ".jpg"))
                im = im_transpose
        method = ['','','','','','','','','',Image.ROTATE_90,Image.ROTATE_180,Image.ROTATE_270]
        sample = random.choice(method)
        if sample:
            for bat in range(batch):
                im_transpose = im.transpose(sample)
                im_transpose.save(os.path.join(savedir, self.gen_random() + ".jpg"))

    def _rotate(self,im,batch,savedir):
        for rotate in range(self.rotate):
            for bat in range(batch):
                im_rotate=im.rotate(rotate)
                im_rotate.save(os.path.join(savedir,self.gen_random()+".jpg"))


if __name__ == "__main__":
    img = r'C:\Users\Administrator\Desktop\photobase_sf\FOUR\1539152586672.jpg'
    img = r'C:\Users\Administrator\Desktop\新增資料夾 (2)\d\f174771.jpg'
    resize(img)